"""
Case Type   : file_fdw功能
Case Name   : file_fdw扩展删除功能验证
Create At   : 2023-11-13
Owner       : peilinqian
Description :
    step1: 系统用户使用两种方式删除file_fdw扩展;
    step2: 设置support_extended_features参数为on，重启数据库;
    step3: 系统用户再次使用两种方式删除file_fdw扩展;
Expect      :
    step1: 系统用户使用两种方式删除file_fdw扩展; expect:删除失败
    step2: 设置support_extended_features参数为on，重启数据库; expect:修改成功
    step3: 系统用户再次使用两种方式删除file_fdw扩展; expect:删除失败
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Filefdw0015(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.err_msg = 'ERROR:  EXTENSION file_fdw does not allow to drop'
        self.com = Common()

    def test_main(self):
        step_txt = '----step0: 查看support_extended_features初始配置值;----'
        self.log.info(step_txt)
        self.init_para = self.com.show_param("support_extended_features")

        step_txt = '----step1: 系统用户使用两种方式删除file_fdw扩展; expect:删除失败----'
        self.log.info(step_txt)
        sql = f"select * from pg_extension where extname= 'file_fdw';" \
            f"drop extension file_fdw;" \
            f"drop extension file_fdw cascade;"
        drop_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_result)
        assert_flag = drop_result.count(self.err_msg)
        self.assertEqual(assert_flag, 2, "执行失败" + step_txt)

        step_txt = '----step2: 设置support_extended_features参数为on，重启数据库; ' \
                   'expect:修改成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"support_extended_features= on")
        self.assertTrue(result, '执行失败:' + step_txt)
        restart = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart, '执行失败:' + step_txt)
        alter_para = self.com.show_param("support_extended_features")
        self.assertEqual(alter_para, 'on', '执行失败:' + step_txt)

        step_txt = '----step3: 系统用户再次使用两种方式删除file_fdw扩展; expect:删除失败----'
        self.log.info(step_txt)
        sql = f"select * from pg_extension where extname= 'file_fdw';" \
            f"drop extension file_fdw;" \
            f"drop extension file_fdw cascade;"
        drop_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_result)
        assert_flag = drop_result.count(self.err_msg)
        self.assertEqual(assert_flag, 2, "执行失败" + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----还原参数; expect:还原成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"support_extended_features="
                                           f"{self.init_para}")
        restart = self.pri_sh.restart_db_cluster()
        alter_para = self.com.show_param("support_extended_features")

        step_txt = '----teardown断言----'
        self.assertTrue(result, '执行失败:' + step_txt)
        self.assertTrue(restart, '执行失败:' + step_txt)
        self.assertEqual(alter_para, self.init_para, '执行失败:' + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
